package org.bedracket.pgeg.datagen.levelgen;

import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.resources.ResourceKey;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;
import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest;
import org.bedracket.pgeg.init.ModBlocks;
import org.bedracket.pgeg.util.Helpers;

import java.util.List;

public class ModConfiguredFeatureProvider {

    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_AIR_INFUSED_STONE =
            register("ore_air_infused_stone");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_AQUA_INFUSED_STONE =
            register("ore_aqua_infused_stone");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_ORDER_INFUSED_STONE =
            register("ore_order_infused_stone");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_EARTH_INFUSED_STONE =
            register("ore_earth_infused_stone");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_ENTROPY_INFUSED_STONE =
            register("ore_entropy_infused_stone");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_FIRE_INFUSED_STONE =
            register("ore_fire_infused_stone");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_AIR_INFUSED_DEEPSLATE =
            register("ore_air_infused_deepslate");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_AQUA_INFUSED_DEEPSLATE =
            register("ore_aqua_infused_deepslate");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_ORDER_INFUSED_DEEPSLATE =
            register("ore_order_infused_deepslate");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_EARTH_INFUSED_DEEPSLATE =
            register("ore_earth_infused_deepslate");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_ENTROPY_INFUSED_DEEPSLATE =
            register("ore_entropy_infused_deepslate");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORE_FIRE_INFUSED_DEEPSLATE =
            register("ore_fire_infused_deepslate");
    public static final ResourceKey<ConfiguredFeature<?, ?>> BRIMSTONE_ORE =
            register("brimstone_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> METEORITE_ORE =
            register("meteorite_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> MITHRIL_ORE =
            register("mithril_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> ORICHALCUM_ORE =
            register("orichalcum_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> SATURNIUM_ORE =
            register("saturnium_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> SILVER_ORE =
            register("silver_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> DEEPSLATE_BRIMSTONE_ORE =
            register("deepslate_brimstone_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> DEEPSLATE_METEORITE_ORE =
            register("deepslate_meteorite_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> DEEPSLATE_MITHRIL_ORE =
            register("deepslate_mithril_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> DEEPSLATE_ORICHALCUM_ORE =
            register("deepslate_orichalcum_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> DEEPSLATE_SATURNIUM_ORE =
            register("deepslate_saturnium_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> DEEPSLATE_SILVER_ORE =
            register("deepslate_silver_ore");

    public static void configuredFeatures(BootstapContext<ConfiguredFeature<?, ?>> context) {
        RuleTest rule1 = new TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES);
        RuleTest rule2 = new TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES);
        context.register(ORE_AIR_INFUSED_STONE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                                ModBlocks.AIR_INFUSED_STONE.get().defaultBlockState())), 8)));
        context.register(ORE_AQUA_INFUSED_STONE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.AQUA_INFUSED_STONE.get().defaultBlockState())), 8)));
        context.register(ORE_ORDER_INFUSED_STONE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.ORDER_INFUSED_STONE.get().defaultBlockState())), 8)));
        context.register(ORE_EARTH_INFUSED_STONE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.EARTH_INFUSED_STONE.get().defaultBlockState())), 8)));
        context.register(ORE_ENTROPY_INFUSED_STONE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.ENTROPY_INFUSED_STONE.get().defaultBlockState())), 8)));
        context.register(ORE_FIRE_INFUSED_STONE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.FIRE_INFUSED_STONE.get().defaultBlockState())), 8)));
        context.register(ORE_AIR_INFUSED_DEEPSLATE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.AIR_INFUSED_DEEPSLATE.get().defaultBlockState())), 8)));
        context.register(ORE_AQUA_INFUSED_DEEPSLATE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.AQUA_INFUSED_DEEPSLATE.get().defaultBlockState())), 8)));
        context.register(ORE_ORDER_INFUSED_DEEPSLATE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.ORDER_INFUSED_DEEPSLATE.get().defaultBlockState())), 8)));
        context.register(ORE_EARTH_INFUSED_DEEPSLATE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.EARTH_INFUSED_DEEPSLATE.get().defaultBlockState())), 8)));
        context.register(ORE_ENTROPY_INFUSED_DEEPSLATE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.ENTROPY_INFUSED_DEEPSLATE.get().defaultBlockState())), 8)));
        context.register(ORE_FIRE_INFUSED_DEEPSLATE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.FIRE_INFUSED_DEEPSLATE.get().defaultBlockState())), 8)));
        context.register(BRIMSTONE_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.BRIMSTONE_ORE.get().defaultBlockState())), 8)));
        context.register(METEORITE_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.METEORITE_ORE.get().defaultBlockState())), 8)));
        context.register(MITHRIL_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.MITHRIL_ORE.get().defaultBlockState())), 8)));
        context.register(ORICHALCUM_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.ORICHALCUM_ORE.get().defaultBlockState())), 8)));
        context.register(SATURNIUM_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.SATURNIUM_ORE.get().defaultBlockState())), 8)));
        context.register(SILVER_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule1,
                        ModBlocks.SILVER_ORE.get().defaultBlockState())), 8)));
        context.register(DEEPSLATE_BRIMSTONE_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.DEEPSLATE_BRIMSTONE_ORE.get().defaultBlockState())), 8)));
        context.register(DEEPSLATE_METEORITE_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.DEEPSLATE_METEORITE_ORE.get().defaultBlockState())), 8)));
        context.register(DEEPSLATE_MITHRIL_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.DEEPSLATE_MITHRIL_ORE.get().defaultBlockState())), 8)));
        context.register(DEEPSLATE_SILVER_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.DEEPSLATE_SILVER_ORE.get().defaultBlockState())), 8)));
        context.register(DEEPSLATE_SATURNIUM_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.DEEPSLATE_SATURNIUM_ORE.get().defaultBlockState())), 8)));
        context.register(DEEPSLATE_ORICHALCUM_ORE, new ConfiguredFeature<>(Feature.ORE,
                new OreConfiguration(List.of(OreConfiguration.target(rule2,
                        ModBlocks.DEEPSLATE_ORICHALCUM_ORE.get().defaultBlockState())), 8)));
    }

    private static ResourceKey<ConfiguredFeature<?, ?>> register(String name) {
        return ResourceKey.create(Registries.CONFIGURED_FEATURE, Helpers.identifier(name));
    }
}
